package de.nordakademie.wpk.share.core.service;

import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.log4j.Logger;

import de.nordakademie.wpk.share.core.data.Depot;
import de.nordakademie.wpk.share.core.data.Share;
import de.nordakademie.wpk.share.core.data.datastorage.depot.DepotContainer;
import de.nordakademie.wpk.share.core.data.datastorage.share.ShareContainer;
import de.nordakademie.wpk.share.core.data.treeitem.TreeGenerator;
import de.nordakademie.wpk.share.core.data.treeitem.TreeItem;

/**
 * Provides access to the data located on the server
 * 
 * @author Christian Baumhof
 * 
 */
public class ShareService implements IShareService {

	private static final Logger logger = Logger.getLogger(ShareService.class);

	public static ShareContainer shareContainer = new ShareContainer();
	public static DepotContainer depotContainer = new DepotContainer();

	/**
	 * Provides a tree containing all shares and the depots in, which they are
	 * contained
	 * 
	 * @return TreeItem
	 */
	@Override
	public TreeItem getShareTree() {
		logger.info("Fetching share tree from server");

		return TreeGenerator.getShareTree();
	}

	/**
	 * Provides a tree containing all depots and indices as well as the
	 * contained shares and a node containing all shares
	 * 
	 * @return TreeItem
	 */
	@Override
	public TreeItem getDepotTree() {
		logger.info("Fetching depot tree from server");

		return TreeGenerator.getDepotTree();
	}

	/**
	 * Provides a list of all available shares
	 * 
	 * @return List of Shares
	 */
	@Override
	public List<Share> getAvailableShares() {
		logger.info("Fetching list of available shares from server");

		return shareContainer.getShares();
	}

	@Override
	public void saveOrUpdateDepot(Depot depot) {
		logger.info("Saving depot");

		depotContainer.saveOrUpdateDepot(depot);
	}

	@Override
	public void deleteDepot(Depot depot) {
		logger.info("Deleting depot from server");

		depotContainer.deleteDepot(depot);
	}

	/**
	 * Returns a new ID for a new Depot
	 * 
	 * @return Integer
	 */
	@Override
	public Integer getUnusedId() {
		logger.info("Fetching unused id from server");

		return depotContainer.getUnusedId();
	}

	/**
	 * Refreshes the server data (only current quotes)
	 */
	@Override
	public void refreshData() {
		logger.info("Refreshing share data");
		shareContainer.refreshCurrentData();
		depotContainer.refreshDepotData();
	}

	/**
	 * Refreshes the server data (only quotes from last 30 days)
	 */
	@Override
	public void refreshHistoricData() {
		shareContainer.refreshHistoricData();
	}

	/**
	 * Returns true, if a depot with the given name already exists, otherwise
	 * false
	 * 
	 * @return Boolean
	 */
	@Override
	public Boolean isUnique(String depotName) {
		return depotContainer.isUnique(depotName);
	}

	/**
	 * Returns a LinkedHashMap containing quotes of the last 30 days for a given
	 * share (identified by symbol)
	 * 
	 * @return LinkedHashMap
	 */
	@Override
	public LinkedHashMap<GregorianCalendar, BigDecimal> getHistoricData(
			String symbol) {
		return shareContainer.getHistoricData(symbol);
	}

}
